package view;

import java.util.ArrayList;

import javax.swing.JOptionPane;

import facade.EditorFacade;

import util.Pair;

public class JFReplace extends javax.swing.JFrame {

	public JFReplace() {
		initComponents();
	}

	public JFReplace(javax.swing.JFrame parent, javax.swing.JTextArea text) {
		this.parent = parent;
		this.text = text;
		initComponents();
	}

	private void initComponents() {

		jLabel1 = new javax.swing.JLabel();
		jTFFind = new javax.swing.JTextField();
		jTFReplace = new javax.swing.JTextField();
		jLabel2 = new javax.swing.JLabel();
		jBFind = new javax.swing.JButton();
		jBReplace = new javax.swing.JButton();
		jBNext = new javax.swing.JButton();
		jBClose = new javax.swing.JButton();
		found = new ArrayList<Pair>();
		ef = new EditorFacade();
		pos = 0;

		setTitle("Substituir");

		jLabel1.setText("Localizar:");

		jLabel2.setText("Substituir por:");

		jBFind.setText("Localizar");
		jBFind.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseReleased(java.awt.event.MouseEvent evt) {
				jBFindMouseReleased(evt);
			}
		});
		jBReplace.setText("Substituir");

		jBNext.setText("Localizar próxima");

		jBClose.setText("Fechar");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.TRAILING)
														.addGroup(
																javax.swing.GroupLayout.Alignment.LEADING,
																layout
																		.createSequentialGroup()
																		.addGroup(
																				layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																						.addComponent(
																								jLabel1)
																						.addComponent(
																								jLabel2))
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addGroup(
																				layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																						.addComponent(
																								jTFReplace,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								284,
																								Short.MAX_VALUE)
																						.addComponent(
																								jTFFind,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								284,
																								Short.MAX_VALUE)))
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addComponent(
																				jBFind)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addComponent(
																				jBNext)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addComponent(
																				jBReplace)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
																		.addComponent(
																				jBClose)))
										.addContainerGap()));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel1)
														.addComponent(
																jTFFind,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																jTFReplace,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(jLabel2))
										.addGap(14, 14, 14)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jBClose)
														.addComponent(jBReplace)
														.addComponent(jBNext)
														.addComponent(jBFind))
										.addContainerGap(
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));

		pack();
	}

	private void jBFindMouseReleased(java.awt.event.MouseEvent evt) {
		String palavra = jTFFind.getText();
		if (palavra != null) {
			found = ef.localizar(text.getText(), palavra);

			Pair p = found.get(next());
			text.select(p.getFstElm(), p.getSndElm());
		}
	}
	
	 private int next() {
		if (this.pos == (this.found.size() - 1)) {
			this.pos = 0;
			return (this.found.size() - 1);
		} else {
			this.pos++;
			return (pos - 1);
		}
	 }
	
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new JFReplace().setVisible(true);
			}
		});
	}

	private javax.swing.JButton jBClose;
	private javax.swing.JButton jBFind;
	private javax.swing.JButton jBNext;
	private javax.swing.JButton jBReplace;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JTextField jTFFind;
	private javax.swing.JTextField jTFReplace;
	private javax.swing.JTextArea text;
	private javax.swing.JFrame parent;
	private ArrayList<Pair> found;
	private static EditorFacade ef;
	private int pos;
}
